package com.hsy.linemoney.modules.exchange;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.MotionEvent;
import android.view.View;

import com.deparse.dpframe.tools.utils.DPUnitUtil;
import com.deparse.dpframe.ui.android.app.fragment.DPBaseFragment;
import com.deparse.dpframe.ui.android.widget.DPViewPager;
import com.deparse.dpframe.ui.android.widget.DPagerSlidingTabStrip;
import com.hsy.linemoney.R;
import com.hsy.linemoney.modules.main.MainActivity;

/**
 * Created by MartinKent on 2016/7/11.
 */
public class ExchangeHomeFragment extends DPBaseFragment {
    protected DPagerSlidingTabStrip tabs;
    protected DPViewPager viewpager;
    private static final Class<?>[] fragments = {
            ExchangeOnceFragment.class,
//            ExchangeMultipleFragment.class
    };
    private static final int[] titles = {
            R.string.txt_exchange_once,
//            R.string.txt_exchange_multiple,
    };
    private MyPagerAdapter myPagerAdapter;

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_exchange_home;
    }

    @Override
    protected void initView(View view, Bundle bundle) {

        tabs = (DPagerSlidingTabStrip) findViewById(R.id.tabs);

        tabs.setIndicatorWidth(DPUnitUtil.dip2px(getAttchedActivity(), 65));

        viewpager = (DPViewPager) findViewById(R.id.viewpager);

        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        viewpager.setAdapter(myPagerAdapter);

        tabs.setViewPager(viewpager);

        tabs.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((MainActivity) getAttchedActivity()).setExchangeFrom(-1);
        ((MainActivity) getAttchedActivity()).setExchangeTo(-1);
        ((MainActivity) getAttchedActivity()).setExchangeAmount("0");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message message) {

    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getResources().getString(titles[position]);
        }

        @Override
        public Fragment getItem(int position) {
            try {
                return (Fragment) fragments[position].newInstance();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }
}
